_joyf        .byte $00
_checkjoy .byte $00


;
; downlimit: e5
;

checkjoy
        lda #1
        sta $d027

        lda $dc00
        tax
        and #1
        beq _up
cj2     txa
        and #2
        beq _down
cj3     txa
        and #4
        beq _left
cj4     txa
        and #8
        beq _right
cjfire  txa
        and #16
        beq _fire
        lda #0
        sta _joyf
cje
       rts

_up     ldy $d001
        cpy #30
        beq cj2
        dec $d001
        dec $d001
        jmp cj2
_down   ldy $d001
        
        cpy #$e4	; check for zero if moving 2 pixels
        beq cj3
        inc $d001
        inc $d001
        jmp cj3
_left   jsr joyckleft
        jmp cj4
_right  jsr joyckright
        jmp cjfire
_fire  lda #7
       sta $d027
       lda _joyf
       bne cje
       jsr checkfire
       jmp cje

joyckleft
         lda $d010
         and #1
         beq ckleft
         lda $d000
         bne leftdec
         dec $d010
     
leftdec  dec $d000
	 dec $d000
cjlr     rts

ckleft   lda $d000
         cmp #24
         beq cjlr
         jmp leftdec

joyckright
         lda $d010
         and #1
         bne ckright
         lda $d000
         cmp #254	; was 255 
         bne rightinc
         inc $d010
rightinc  inc $d000
	 inc $d000
cjrr     rts
ckright   lda $d000
         cmp #344-256-24
         beq cjrr
         jmp rightinc

checkfire




         inc _joyf
;paus : 215x2   - 245x32

fpaus    lda $d000
         cmp #215+24
         bcc fplay
         cmp #255;+24
         bpl fplay
         lda $d001
         cmp #2+50
         bcc fplay
         cmp #32+50
         bpl fplay
         jsr reset_sid
         ;lda graj
         ;eor #1
         ;sta graj
         lda #135	;  <-
         sta _checkjoy

         jmp fkoniec

; to avoid calling routines inside URQ (timing fucks up)
; checkjoy flag is set accoeding to areas where user pressed button
; and then the routine is called in main loop outside IRQ

;play
fplay    lda $d000
         cmp #100+24
         bcc fstop
         cmp #130+24
         bpl fstop
         lda $d001
         cmp #40+50
         bcc fstop
         cmp #70+50
         bpl fstop
;	jsr play
         lda #133	; 133=f1
         sta _checkjoy
         jmp fkoniec
;stop
fstop    lda $d000
         cmp #130+24
         bcc ftload0
         cmp #160+24
         bpl ftload0
         lda $d001
         cmp #25+50
         bcc ftload0
         cmp #55+50
         bpl ftload0
;         jsr stop
         lda #134	; f4
         sta _checkjoy

; x105 y175 , size of the full box: 145x55

ftload0
	 lda $d000
         cmp #105
         bcc ftload1
         cmp #240
         bpl ftload1
         lda $d001
         cmp #190
         bcc ftload1
         cmp #204
         bpl ftload1
;         jsr stop
         lda #136	; f7 - load
         sta _checkjoy
	lda #0
	sta selline	; selected line

ftload1
	 lda $d000
         cmp #105
         bcc ftload2
         cmp #240
         bpl ftload2
         lda $d001
         cmp #205
         bcc ftload2
         cmp #219
         bpl ftload2
;         jsr stop
         lda #136	; f7 - load
         sta _checkjoy
	lda #1
	sta selline	; selected line


ftload2
	 lda $d000
         cmp #105
         bcc fup
         cmp #240
         bpl fup
         lda $d001
         cmp #220
         bcc fup
         cmp #234
         bpl fup
;         jsr stop
         lda #136	; f7 - load
         sta _checkjoy
	lda #2
	sta selline	; selected line



fup      lda $d010
	 and #1
	 bne fkoniec
	
	 lda $d000
         cmp #50+24
         bcc fdn
         cmp #70+24
         bpl fdn
         lda $d001
         cmp #130+50
         bcc fdn
         cmp #155+50
         bpl fdn
;         jsr zakminus
;         jsr rysuj
	lda #17
	sta _checkjoy

fdn      lda $d000
         cmp #50+24
         bcc fjbox
         cmp #70+24
         bpl fjbox
         lda $d001
         cmp #175+50
         bcc fjbox
         cmp #197+50
         bpl fjbox
         ;jsr zakplus
         ;jsr rysuj
         lda #29
         sta _checkjoy         

fjbox      lda $d000
         cmp #240
         bcc fkoniec
         cmp #254
         bpl fkoniec
         lda $d001
         cmp #247 ;175+50
         bcc fkoniec
         cmp #254
         bpl fkoniec
         ;jsr zakplus
         ;jsr rysuj
         lda #95	; <- arrow
         sta _checkjoy         


fkoniec

           _cv
                rts
